package 超市;

import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.Scanner;

public class Main {
    static int N = 10010;
    static PII[] p = new PII[N];
    static PriorityQueue<Integer> heap = new PriorityQueue<>();

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextInt()) {
            int n = in.nextInt();
            for (int i = 0; i < n; i++) {
                int a = in.nextInt();
                int b = in.nextInt();
                p[i] = new PII(a, b);
            }
            Arrays.sort(p, 0, n);
            for (int i = 0; i < n; i++) {
                heap.offer(p[i].x);
                if (heap.size() > p[i].y) {
                    heap.poll();
                }
            }
            int res = 0;
            while (!heap.isEmpty()) {
                res += heap.poll();
            }
            System.out.println(res);
        }
    }
}

class PII implements Comparable<PII> {
    int x, y;//x->利润;y->过期时间

    public PII(int x, int y) {
        this.x = x;
        this.y = y;
    }

    @Override
    public int compareTo(PII o) {
        return this.y - o.y;
    }
}